var WxSearch = require('../wxSearch/wxSearch.js')
const app = getApp()
const api = require('../../../utils/api.js');
const util = require('../../../utils/util.js');
// pages/home/home.js
Page({
  searchstring: "",


  /**
   * 页面的初始数据
   */
  data: {
    focus: true,
    searchstring: '',
    dianpu: {
      items: []
    },
    sousuo: {
      items: []
    },
    wxSearchData: {
      view: {
        isShow: true
      }
    }
  },
  //获取用户输入的用户名
  bindKeyInput: function (e) {

  },



  toDetail: function (e) {
    //根据taobao_link字段的有无判断是淘宝或精品商品
    //再传与后台值的时候 ds:0 表示精品 ds:1表示淘宝
    console.log("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%开始跳转本次搜索内容为：" + this.data.searchstring);
    let ds = e.currentTarget.dataset.hi;
    let goods_id = e.currentTarget.id;
    if (ds == "huiguo") {
      wx.navigateTo({
        url: `/pages/buy/detail/selfEmployed/selfEmployed?goods_id=` + goods_id,
      });
    } else if (ds == "meitianhui") {
      wx.navigateTo({
        url: `/pages/buy/detail/selfsupport/selfsupport?goods_id=` + goods_id,
      });
    }
    else {
      wx.navigateTo({
        url: `/pages/buy/detail/taotaoling/taotaoling?goods_id=` + goods_id,
      });
    }

  },


  onLoad: function () {
    console.log('onLoad')
    var that = this
    //初始化的时候渲染wxSearchdata
    WxSearch.init(that, 43, ['weappdev', '小程序', 'wxParse', 'wxSearch', 'wxNotification']);
    WxSearch.initMindKeys(['weappdev.com', '微信小程序开发', '微信开发', '微信小程序']);

  },

  wxSearchFn: function (e) {
    var that = this
    WxSearch.wxSearchAddHisKey(that);
    console.log("搜索内容为=============：" + this.data.searchstring);
    var that = this;
    const data = {
      //测试环境默认设置的
      page: '{"page_size":"20","page_no":"1"}',
      service: 'psGoods.smallprogram.freeGetGoodsSearchListPageFind',
      params: '{"search":"' + this.data.searchstring + '"}',
      url_type: 'goods',

      app_token: app.globalData.app_token,
      security_code: app.globalData.security_code

    }

    console.log("+++++++++++++++++++++++");
    console.log(data);

    const sousuo = this.data.sousuo

    api.reqData({
      data,
      success: (res) => {
        console.log("success 返回 res.data>>>" + res.data);
        var searchData = res.data;

        console.log(searchData);
        console.log(">>>>>>>res.data>>>");
        sousuo.items = searchData.data.list;
        //dianpu.items = [...dianpu.items, ...resData.data.list]
        console.log("sousuo>>>" + sousuo);

        for (var i = 0; i < sousuo.items.length; i++) {
          sousuo.items[i].taobao_price = (sousuo.items[i].market_price - sousuo.items[i].discount_price).toFixed(2);

          sousuo.items[i].pic_info_url = sousuo.items[i].pic_info_url.replace("https", "http")
          console.log(sousuo.items[i].pic_info_url)

        }

        this.setData({
          sousuo: sousuo,
          searchstring: this.data.searchstring,

        })
        console.log("@@@@@@@@@@@@@@@@@@@@@")
        console.log(sousuo)
        console.log(this.data.sousuo)
        console.log("@@@@@@@@@@@@@@@@@@@@@")
      },
      fail: (res) => {
        console.log("fail 返回 res.data>>>" + res.data);
      },
      complete: (res) => {
        console.log("complete 返回 res.data>>>" + res.data);
      }
    })
  },


  //确定时完成的事件
  wxSearchfirm: function (e) {
    var that = this
    this.setData({
      searchstring: e.detail.value
    })
    this.wxSearchFn();
    WxSearch.wxSearchAddHisKey(that);
  },
  //当键盘输入时，触发input事件
  wxSearchInput: function (e) {
    var that = this
    WxSearch.wxSearchInput(e, that);
  },
  //输入框聚焦时触发
  wxSerchFocus: function (e) {
    var that = this
    WxSearch.wxSearchFocus(e, that);
  },
  //输入框失去焦点时触发
  // wxSearchBlur: function (e) {
  //   var that = this
  //   WxSearch.wxSearchBlur(e, that);
  // },
  wxSearchKeyTap: function (e) {
    var that = this
    this.setData({
      searchstring: e.currentTarget.dataset.key
    })
    this.wxSearchFn();
    WxSearch.wxSearchKeyTap(e, that);
  },
  wxSearchDeleteKey: function (e) {
    var that = this
    WxSearch.wxSearchDeleteKey(e, that);
  },
  wxSearchDeleteAll: function (e) {
    var that = this;
    WxSearch.wxSearchDeleteAll(that);
  },
  wxSearchTap: function (e) {
    var that = this
    WxSearch.wxSearchHiddenPancel(that);
  }
})